import { ApiCall } from "tsrpc";
import { player } from "../../../model/fight/body/player";
import { SK_type } from "../../../shared/interface";
import { ReqFreeSkill, ResFreeSkill } from "../../../shared/master/body/PtlFreeSkill";
import { template } from "../../../shared/master/MsgAction";

export default async function (call: ApiCall<ReqFreeSkill, ResFreeSkill>) {
    let user = call.req._player as player;
    call.succ({})
    let sks = user.getAllSkills();
    let useName = call.req.skName;
    let _idx = sks.findIndex((item)=>{
        return item.rename == useName;
    })
    if(_idx == -1){
        user.sendMsg(template.default,`你还未习得名称为[${useName}]技能`);
        return;
    }
    
    let useSk = sks[_idx];
    if(useSk.type == SK_type.被动技能){
        user.sendMsg(template.default,`[${useName}]为被动技能，无需主动使用`);
        return;
    }
    let cdTime = useSk.getCdTime();
    if(cdTime > 0){
        user.sendMsg(template.default,`[${useName}]技能未就绪.剩余${cdTime}秒`);
        return;
    }
    if(!useSk.checkUseUp()){
        user.sendMsg(template.default,`[${useName}]技能所需消耗不足`);
        return;
    }
    
   
    user.setSelectSkill(useName);
    user.sendMsg(template.default,`${useSk.rename}Lv.${useSk.leve.num},技能将会在之后的战斗回合优先使用.`)

}